﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */


#region Using
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ComponentModel;
using NetBay.Core.BusinessEntities.Enum;
using NetBay.Core.BusinessEntities.DashBoard;
using NetBay.Core.BusinessEntities.Versioning;
using NetBay.Core.BusinessEntities.TaskManager;
using NetBay.Core.BusinessEntities.AdvancedSearchCriteria;
using NetBay.Core.BusinessEntities.Historic;
using NetBay.Core.BusinessEntities.Workflow;
using NetBay.Core.BusinessEntities.Connectors;
using NetBay.Core.BusinessEntities.Barcodes;
using NetBay.Core.BusinessEntities.ActiveDirectory;
using NetBay.Core.Encryption.Licensing;
#endregion

namespace NetBay.Core.BusinessEntities
{
    [DataContract]
    [KnownType(typeof(User))]
    [KnownType(typeof(Group))]
    [KnownType(typeof(Organization))]
    [KnownType(typeof(Index))]
    [KnownType(typeof(FolderType))]
    [KnownType(typeof(Folder))]
    [KnownType(typeof(Document))]
    [KnownType(typeof(Item))]
    [KnownType(typeof(Vignette))]
    [KnownType(typeof(Category))]
    [KnownType(typeof(ItemRights))]
    [KnownType(typeof(Cart))]
    [KnownType(typeof(ImageRio))]
    [KnownType(typeof(MessageFlow))]
    [KnownType(typeof(Constant))]
    [KnownType(typeof(OccurenciesForOneIndex))]
    [KnownType(typeof(OccurenciesForAllIndex))]
    [KnownType(typeof(OccurenciesByFolderType))]
    [KnownType(typeof(OccurenciesByFolder))]
    [KnownType(typeof(ReturnBoolean))]
    [KnownType(typeof(ReturnByte))]
    [KnownType(typeof(ReturnString))]
    [KnownType(typeof(ReturnLong))]
    [KnownType(typeof(StateMessageFlow))]
    [KnownType(typeof(ParamMessageFlow))]
    [KnownType(typeof(StateDoc))]
    [KnownType(typeof(ReturnDoubleValue))]
    [KnownType(typeof(Alert))]
    [KnownType(typeof(ReturnDictionary))]
    [KnownType(typeof(OcrMask))]
    [KnownType(typeof(OcrMaskItem))]
    [KnownType(typeof(OcrArea))]
    [KnownType(typeof(OcrItem))]
    [KnownType(typeof(OcrType))]
    [KnownType(typeof(NumerizationOperator))]
    [KnownType(typeof(NumerizationResum))]
    [KnownType(typeof(NumerizationOperatorByDate))]
    [KnownType(typeof(NumerizationFoldersByDate))]
    //--Versioning
    [KnownType(typeof(VersioningModel))]
    [KnownType(typeof(VersioningItem))]
    [KnownType(typeof(VersionState))]
    [KnownType(typeof(VersionNumber))]
    //--Task
    [KnownType(typeof(TaskItem))]
    [KnownType(typeof(AdvancedSearchCriteriaItem))]
    //Historic
    [KnownType(typeof(HistoricIndex))]
    [KnownType(typeof(HistoricInformation))]
    //GeneralParameter
    [KnownType(typeof(GeneralParameter))]
    [KnownType(typeof(GeneralParameterDomain))]
    [KnownType(typeof(GeneralParameterName))]
    [KnownType(typeof(GeneralParameterType))]
    //Workflow
    [KnownType(typeof(WorkflowModel))]
    [KnownType(typeof(WorkflowDefinition))]
    [KnownType(typeof(WorkflowAction))]
    [KnownType(typeof(WorkflowCondition))]
    [KnownType(typeof(ReceiverType))]
    [KnownType(typeof(WorkflowInstance))]
    [KnownType(typeof(WorkflowState))]
    [KnownType(typeof(ReceiverType))]
    [KnownType(typeof(WorkflowInstanceUser))]
    [KnownType(typeof(WorkflowHisto))]
    [KnownType(typeof(WorkflowItem))]
    //Connector
    [KnownType(typeof(Connector))]
    [KnownType(typeof(ConnectorCorrespondence))]
    [KnownType(typeof(ConnectorParameters))]
    [KnownType(typeof(Genesis))]
    //Barcode
    [KnownType(typeof(Barcode))]
    [KnownType(typeof(BarcodeResultItem))]
    [KnownType(typeof(BarcodeSeparator))]
    [KnownType(typeof(BarcodeDocumentSeparator))]
    [KnownType(typeof(BarcodeFolderSeparator))]
    [KnownType(typeof(BarcodeSeparatorType))]
    [KnownType(typeof(BarcodeType))]
    
    //Licencing
    //[KnownType(typeof(LicenseFile))]
    [KnownType(typeof(LicenseTerms))]
    [KnownType(typeof(LicenseAuthorizationBase))]
    //[KnownType(typeof(VersionInfo))]
    //[KnownType(typeof(LicenseType))]
    

    [Serializable]
    public class ReturnObject : ItemEntity
    {
        #region Enum
        /// <summary>
        /// List of Error codes
        /// </summary>
        public enum ErrorCodes
        {
            /// <summary>
            /// Success, all ok
            /// </summary>
            [DescriptionAttribute("Process succeed")]
            Success = 0,

            /*
             * General Errors
             */

            /// <summary>
            /// Server not found
            /// </summary>
            [DescriptionAttribute("Server not found")]
            ServerNotFound = 9001,
            /// <summary>
            /// An unknow error
            /// </summary>
            [DescriptionAttribute("An unknow error has occured")]
            UnknowError = 9002,
            /// <summary>
            /// Internal Error
            /// <remarks>
            /// Generaly when a code try to store a unknown error code
            /// </remarks>
            /// </summary>
            [DescriptionAttribute("Internal Error")]
            InternalError = 9003,
            /// <summary>
            /// The file is corrupted.
            /// </summary>
            [DescriptionAttribute("The current file is corrupted")]
            CorruptedFile = 9004,
            /// <summary>
            /// No data
            /// </summary>
            [DescriptionAttribute("No data found")]
            NoData = 9005,
            /// <summary>
            /// The file doesn't exist.
            /// </summary>
            [DescriptionAttribute("File not found.")]
            FileNotFound = 9006,

            /*
             * Import Mode
             */

            /// <summary>
            /// An error has occured while deleting a file in the import mode.
            /// </summary>
            [DescriptionAttribute("An error has occured while deleting a file in the import mode")]
            DeletingFileDuringImportError = 9101,
            /// <summary>
            /// An error has occured while rotating a file in the import mode.
            /// </summary>
            [DescriptionAttribute("An error has occured while rotating a file in the import mode")]
            RotationFileDuringImportError = 9102,
            /// <summary>
            /// An error has occured while saving a file in the import mode.
            /// </summary>
            [DescriptionAttribute("An error has occured while saving a file in the import mode")]
            SaveFileDuringImportError = 9103,
            /// <summary>
            /// An error has occured while merging a file in the import mode.
            /// </summary>
            [DescriptionAttribute("An error has occured while merging a file in the import mode")]
            MergeFileDuringImportError = 9104,
        }
        #endregion
        
        #region Public
        /// <summary>
        /// 
        /// </summary>
        public static readonly int SUCCESS = 0;
        #endregion

        #region Private members
        /// <summary>
        /// Base is success
        /// </summary>
        //private int m_iErrorCode = ReturnObject.SUCCESS;
        private int m_iErrorCode = (int)ErrorCodes.Success;
        /// <summary>
        /// Error Code
        /// </summary>
        private Exception m_oApplicationException;
        /// <summary>
        /// The error message
        /// </summary>
        private string m_sErrorMessage;
        /// <summary>
        /// Error Message
        /// </summary>
        private List<string> m_lstErrorMessageParameters;
        /// <summary>
        /// Is valid or not
        /// </summary>
        private bool m_bIsValid;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the error code.
        /// </summary>
        /// <value>The error code.</value>
        [DataMember]
        public int ErrorCode
        {
            get { return m_iErrorCode; }
            set
            {
                m_iErrorCode = value;
            }
        }
        /// <summary>
        /// Gets or sets the application exception.
        /// </summary>
        /// <value>The application exception.</value>
        [DataMember]
        public Exception ApplicationException
        {
            get { return m_oApplicationException; }
            set
            {
                m_oApplicationException = value;
                m_bIsValid = false;
            }
        }
        /// <summary>
        /// Gets or sets the error message.
        /// </summary>
        /// <value>The erro message.</value>
        [DataMember]
        public string ErrorMessage
        {
            get { return m_sErrorMessage; }
            set
            {
                m_sErrorMessage = value;
                // If no erro message the object is valid
                if (!string.IsNullOrEmpty(m_sErrorMessage))
                {
                    m_bIsValid = false;
                }
                else
                {
                    IsValid = true;
                }
            }
        }
        /// <summary>
        /// Gets the full error message (Error message with exception message)
        /// </summary>
        /// <value>The full error message</value>
        [DataMember]
        public string FullErrorMessage
        {
            get 
            {
                if (!string.IsNullOrEmpty(m_sErrorMessage))
                    return m_sErrorMessage + " EXCEPTION -> " + Environment.NewLine + ApplicationException;
                else
                    return string.Empty;
            }
            set
            {
                m_sErrorMessage = value;
            }
        }
        /// <summary>
        /// Gets or sets the error message parameters.
        /// </summary>
        /// <value>The error message parameters.</value>
        [DataMember]
        public List<string> ErrorMessageParametersList
        {
            get
            {
                if (m_lstErrorMessageParameters == null)
                {
                    m_lstErrorMessageParameters = new List<string>();
                }
                return m_lstErrorMessageParameters;
            }
            set
            {
                m_lstErrorMessageParameters = value;
            }
        }
        /// <summary>
        /// Gets or sets a value indicating whether this instance is valid.
        /// </summary>
        /// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
        [DataMember]
        public bool IsValid
        {
            get { return m_bIsValid; }
            set { m_bIsValid = value; }
        }
        #endregion

        #region Method
        /// <summary>
        /// Adds the error message parameter.
        /// </summary>
        /// <param name="param">The param.</param>
        public void AddErrorMessageParameter(string param)
        {
            if (m_lstErrorMessageParameters == null)
            {
                m_lstErrorMessageParameters = new List<string>();
            }
            m_lstErrorMessageParameters.Add(param);
        }
        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString()
        {
            string message = string.Empty;
            if (this.m_oApplicationException != null)
            {
                if (string.IsNullOrEmpty(this.m_sErrorMessage))
                {
                    message = this.m_oApplicationException.Message;
                }
                else
                {
                    message = this.m_sErrorMessage;
                }
                return string.Format("({0}): {1}",
                    message,
                    this.m_oApplicationException);
            }
            else
            {
                return string.Format("({0})",
                    this.m_sErrorMessage);
            }
        }
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"></see> to compare with the current <see cref="T:System.Object"></see>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>; otherwise, false.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is ReturnObject))
                return false;
            ReturnObject tmp = (obj as ReturnObject);
            return (this.IsValid.Equals(tmp.m_bIsValid)
                && this.ErrorMessage == tmp.ErrorMessage);
        }
        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        /// <summary>
        /// Clears the return object value to its initial one
        /// </summary>
        public void Clear()
        {
            this.m_bIsValid = true;
            this.m_sErrorMessage = string.Empty;
            this.m_iErrorCode = SUCCESS;
            this.m_oApplicationException = null;
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorObject"/> class.
        /// </summary>
        public ReturnObject()
        {
            this.m_bIsValid = true;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorObject"/> class.
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <param name="applicationException">The application exception.</param>
        public ReturnObject(string errorMessage, Exception applicationException=null)
        {
            this.m_bIsValid = false;
            this.m_sErrorMessage = errorMessage;
            this.m_oApplicationException = applicationException;
        }
        #endregion

        public override object Clone()
        {
            throw new NotImplementedException();
        }
    }
   
}
